package cn.javadog.cloud.starter.demo.config;

import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.javadog.cloud.starter.limiter.exception.LimiterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author yuy
 * @date 2020-11-06 17:26
 *
 * 全局异常
 **/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ResponseBody
    @ExceptionHandler(LimiterException.class)
    public JSONObject handleLimiterException(LimiterException ex) {
        log.warn("请求频率超过限制");
        JSONObject result = new JSONObject();
        result.set("status", HttpStatus.HTTP_FORBIDDEN);
        result.set("error_msg", "请求频率超过限制");
        return result;
    }

}
